import { loginAndRegister } from "../../../utils/api/module/login"

Page({
    data: {
        phAccount: "请输入手机号",   //输入账号提示信息
        accountVal: null, //输入的账号
        phPassword: "请输入密码",   //输入密码提示信息
        passwordVal: null,   //输入的密码
        // 本页所有的图片
        imgs: {
            bgImg: "https://ythapi.bjyangshu.com/public/images/login-bg-img.png",
        },
        forgetPath: "/pages/loginAndRegister/forgetPassword/index",
        registerPath: "/pages/loginAndRegister/register/index",
        noAccountPath: "/pages/loginAndRegister/noAccount/index",
        userAgreementPath:'/pages/loginAndRegister/userAgreement/index',
    },
    onLoad: function (options) {

    },
    /**
     * 双向绑定账号value
     * @param e
     */
    getAccountVal(e){
       this.setData({ accountVal: e.detail.value})
    },
    /**
     * 双向绑定密码value
     * @param e
     */
    getPasswordVal(e){
        this.setData({ passwordVal: e.detail.value})

    },
    /**
     * click login button
     */
    submitLogin(){
        let accountVal =this.data.accountVal;
        let passwordVal = this.data.passwordVal;
        if(accountVal == null){
            wx.showToast({
                title: "账号不能为空",
                icon: "error"
            });
            return false
        }
        if(passwordVal == null){
            wx.showToast({
                title: "密码不能为空",
                icon: "error"
            });
            return false
        }
        /*api登录*/
        loginAndRegister({
            type: 1,
            mobile : accountVal,
            password : passwordVal,
            messagecode :'',
        }).then(res => {
            console.log(res);
            if(res.data.code == 1){
                //设置sessionid
                wx.setStorageSync("session_id", res.data.sessionid);
                //设置为登录状态
                wx.setStorageSync('isLogin', true);
                wx.setStorageSync('userInfo', res.data.data);
                wx.setStorageSync('userInfoData1', res.data);
                wx.setStorageSync('loginTimer', (new Date).getTime());
                wx.switchTab({
                    url:'/pages/enterprise/index/index',
                })
            }else if (res.data.code == 999 ){
                wx.showToast({ title: res.data.message,icon: "none"});
            }else if(res.data.code == 0){
                wx.showToast({title: res.data.message, icon: "none"});
            }
        })
    },
    /**
     * 使用微信登录
     */
    getUserInfo(e){
        if(e.detail.errMsg != "getUserInfo:ok"){
            console.log("拒绝授权了");
            return false;
        }
        let userInfo =e.detail.userInfo;
        console.log(userInfo);
    }
});